from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from first_info import create_app, db
from first_info.models import User

app = create_app('dev')

# 创建脚本管理器对象
manager = Manager(app)
# ctrl + p 查看需要传入的参数提醒

Migrate(app, db)
# 第一个参数是别名，第二个参数固定
manager.add_command('huangli', MigrateCommand)


@manager.option('-u', '-username', dest='username')
@manager.option('-p', '-password', dest='password')
@manager.option('-m', '-mobile', dest='mobile')
def createsuperadmin(username, password, mobile):
    if not all([username, password, mobile]):
        print('参数不全')
    # 创建对象
    admin = User()
    admin.nick_name = username
    admin.password = password
    admin.mobile = mobile
    admin.is_admin = True
    try:
        db.session.add(admin)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        print(e)
if __name__ == '__main__':
    # print(sys.path)
    print(app.url_map)
    manager.run()

